﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sport
{
	public class SoccerEvent : SportEvent
	{
		public int HomePoint
		{
			get
			{
				return score_home > score_away ? 3 : score_home == score_away ? 1 : 0;
			}
		}
		public int AwayPoint
		{
			get
			{
				return score_home < score_away ? 3 : score_home == score_away ? 1 : 0;
			}
		}
		public SoccerEvent(int iround, SportTeam ihome, SportTeam iaway, DateTime itime)
			: base(iround, ihome, iaway, itime)
		{
		}
	}
	public class SoccerEntry : TableEntry
	{
		public int win, draw, lost;
		public int GoalFor, GoalAgainst;
		public int GoalDiff
		{
			get
			{
				return GoalFor - GoalAgainst;
			}
		}
		public SoccerEntry(SportTeam iteam)
			: base(iteam)
		{
		}
		public override void CompleteEntry()
		{
			win = draw = lost = 0;
			GoalFor = GoalAgainst = 0;
			foreach (SoccerEvent se in events)
			{
				bool ishome = se.home == team;
				points += ishome ? se.HomePoint : se.AwayPoint;
				GoalFor += ishome ? se.score_home : se.score_away;
				GoalAgainst += ishome ? se.score_away : se.score_home;
				if (se.HomePoint == 3)
				{
					if (ishome)
						win++;
					else
						lost++;
				}
				else if (se.HomePoint == 1)
					draw++;
				else
				{
					if (ishome)
						lost++;
					else
						win++;
				}
			}
		}

		public override string ToString()
		{
			return team.name.PadRight(20) + win + "-" + draw + "-" + lost + " " + GoalFor + ":" + GoalAgainst + " " + GoalDiff + "   " + points.ToString().PadLeft(2);
		}
	}
	class PointComparer : IComparer<TableEntry>
	{
		public int Compare(TableEntry e1, TableEntry e2)
		{
			return -e1.points.CompareTo(e2.points);
		}
	}
	class ScorediffComparer : IComparer<TableEntry>
	{
		public int Compare(TableEntry e1, TableEntry e2)
		{
			SoccerEntry f1 = e1 as SoccerEntry;
			SoccerEntry f2 = e2 as SoccerEntry;
			return -f1.GoalDiff.CompareTo(f2.GoalDiff);
		}
	}
	public class SoccerTable : SportTableBase
	{
		protected override SportEvent CreateEvent(int round, SportTeam t1, SportTeam t2, DateTime itime)
		{
			return new SoccerEvent(round, t1, t2, itime);
		}
		public override TableEntry CreateEntry(SportTeam iteam)
		{
			return new SoccerEntry(iteam);
		}
		public override void Sort(int stage, int first, int cnt)
		{
			IComparer<TableEntry> comp = null;
			String S = "";
			switch (stage)
			{
				case 0:
					comp = new PointComparer();
					S = "Point equals";
					break;
				case 1:
					comp = new ScorediffComparer();
					S = "Score equals";
					break;
			}
			if (stage == 2)
			{
				Console.WriteLine("Tie");
				return;
			}
			table.Sort(first, cnt, comp);
			int N1 = first;
			int N2 = first + 1;
			while (N2 <= first + cnt)
			{
				if (N2 == first + cnt || comp.Compare(table[N1], table[N2]) != 0)
				{
					if (N2 > N1 + 1)
					{
						Console.WriteLine(S + N1 + "-" + (N2 - 1));
						Sort(stage + 1, N1, N2 - N1);
					}
					N1 = N2;
				}
				N2++;
			}
		}
	}
}